// led.c
#include "led.h"
#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_io.h"
#include "hi_i2c.h"
#include  "hi_types_base.h"
#define PCF8574_ADDR 0x42
void led_init(void) {
    int ret;
    //设置IO口，IO10是SDA
    //IO9是SCL
    ret=hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_I2C0_SDA);
    
    if(ret!=0)
    {
        printf("set gpio 10 failed\n");
       return;
    } 
    ret=hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_I2C0_SCL);
    
    if(ret!=0)
    {
        printf("set gpio 9 failed\n");
         return;
    }
    ret=hi_io_set_pull(HI_IO_NAME_GPIO_10, HI_IO_PULL_UP);
    if(ret!=0)
    {
        printf("set gpio 10 up failed\n");
       return;
    } 
    ret=hi_io_set_pull(HI_IO_NAME_GPIO_9, HI_IO_PULL_UP);
    if(ret!=0)
    {
        printf("set gpio 9 up failed\n");
       return;
    } 
    //初始化i2c
    hi_i2c_init(HI_I2C_IDX_0,I2C_RATE_DEFAULT);
    if(ret!=0)
    {
        printf("Init i2c failed\n");
       return;
    } 
    //把所有器件关闭
    led_off();
    printf("led init sucess\n");
}
//打开led
void led_on(hi_u8 value) {
    hi_i2c_data data;
    int ret;
    data.send_buf=&value;
    data.send_len=1;
    ret=hi_i2c_write(HI_I2C_IDX_0,PCF8574_ADDR,&data);
    if(ret!=0)
    {
        printf("write buf failed\n");
    }
    printf("led on\n");
}

void led_off(void) {
    hi_i2c_data data;
    hi_u8 buf=6;
    int ret;
    data.send_buf=&buf;
    data.send_len=1;
    ret=hi_i2c_write(HI_I2C_IDX_0,PCF8574_ADDR,&data);
    if(ret!=0)
    {
        printf("write i2c failed\n");
    }
    printf("led off\n");
}